﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Warranty.Frm.ModelView;
using Warranty.Core.IService;
using FX.Core;
using Warranty.Core.Domain;

namespace Warranty.Frm
{
    public partial class BanHang : Form
    {
        private readonly IStockService _StockSrv;
        private readonly IProductService _ProductSrv;
        private readonly IDeliveryNoteService _DeliveryNoteSrv;
        private readonly IDeliveryNoteDetailService _DeliveryNoteDetailSrv;
        List<DeliveryNoteDetailView> rs;
        public BanHang()
        {
            InitializeComponent();
            _StockSrv = IoC.Resolve<IStockService>();
            _ProductSrv = IoC.Resolve<IProductService>();
            _DeliveryNoteSrv = IoC.Resolve<IDeliveryNoteService>();
            _DeliveryNoteDetailSrv = IoC.Resolve<IDeliveryNoteDetailService>();

            dataGridView1.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn c1 = new DataGridViewTextBoxColumn();
            c1.DataPropertyName = "ID";
            c1.Visible = false;

            DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn();
            c2.DataPropertyName = "IdProduct";
            c2.Visible = false;

            DataGridViewTextBoxColumn c3 = new DataGridViewTextBoxColumn();
            c3.DataPropertyName = "NameProduct";
            c3.HeaderText = "Tên";

            DataGridViewTextBoxColumn c4 = new DataGridViewTextBoxColumn();
            c4.DataPropertyName = "Price";
            c4.HeaderText = "Đơn giá";

            DataGridViewTextBoxColumn c5 = new DataGridViewTextBoxColumn();
            c5.DataPropertyName = "Quantity";
            c5.HeaderText = "Số lượng";


            DataGridViewButtonColumn bcol = new DataGridViewButtonColumn();
            bcol.HeaderText = " ";
            bcol.Text = "Xóa";
            bcol.Name = "btnClickMe";
            bcol.UseColumnTextForButtonValue = true;

            dataGridView1.Columns.Add(c1);
            dataGridView1.Columns.Add(c2);
            dataGridView1.Columns.Add(c3);
            dataGridView1.Columns.Add(c4);
            dataGridView1.Columns.Add(c5);
            dataGridView1.Columns.Add(bcol);
            dataGridView1.Columns[5].Width = 40;
            this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.ReadOnly = true;
        }

        private void BanHang_Load(object sender, EventArgs e)
        {
            rs = new List<DeliveryNoteDetailView>();
            List<Product> a = _ProductSrv.GetAll().ToList();
            cbbProduct.DataSource = a;
            cbbProduct.DisplayMember = "Name";
            cbbProduct.ValueMember = "ID";
        }
        public void AddItemToGrid()
        {
            bool check = false;
            if (ValidateForm())
            {
                foreach (DeliveryNoteDetailView item in rs)
                {
                    if (item.IdProduct == Convert.ToInt32(cbbProduct.SelectedValue))
                    {
                        item.Quantity = (Convert.ToInt32(item.Quantity) + Convert.ToInt32(txtQuantity.Text)).ToString();
                        check = true;
                    }
                }
                if (check == false)
                {
                    DeliveryNoteDetailView itemnew = new DeliveryNoteDetailView();
                    itemnew.IdProduct = Convert.ToInt32(cbbProduct.SelectedValue);
                    Product a = _ProductSrv.Getbykey(Convert.ToInt32(cbbProduct.SelectedValue));
                    itemnew.NameProduct = a.Name;
                    itemnew.Price = a.Price;
                    itemnew.Quantity = txtQuantity.Text;
                    rs.Add(itemnew);

                }
                txtQuantity.ResetText();
                txtQuantity.BackColor = Color.White;
                refreshDataGrid();
            }
        }
        private void refreshDataGrid()
        {

            dataGridView1.DataSource = typeof(List<>);
            dataGridView1.DataSource = rs;
            dataGridView1.AutoResizeColumns();
            dataGridView1.Refresh();
        }
        public bool ValidateForm()
        {
            string Str = txtQuantity.Text.Trim();
            double Num;
            bool isNum = double.TryParse(Str, out Num);
            if (!isNum || double.Parse(Str) < 0)
            {
                MessageBox.Show("Sai định dạng");
                txtQuantity.BackColor = Color.Red;
                txtQuantity.Focus();
                return false;
            }
            return true;
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            AddItemToGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DeliveryNote ms = new DeliveryNote();
            ms.Notes = "123123";
            ms.Time = DateTime.Now;
            ms.ClientName = txtClientName.Text;
            ms.Address = txtAddress.Text;
            DeliveryNote a = _DeliveryNoteSrv.Save(ms);

            Int64 totalprice = 0;
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string idproduct = dataGridView1.Rows[i].Cells[1].Value.ToString();
                string nameproduct = dataGridView1.Rows[i].Cells[2].Value.ToString();
                string price = dataGridView1.Rows[i].Cells[3].Value.ToString();
                string quantity = dataGridView1.Rows[i].Cells[4].Value.ToString();
                DeliveryNoteDetail dt = new DeliveryNoteDetail();
                dt.IdDeliveryNote = a.ID;
                dt.IdProduct = Convert.ToInt32(idproduct);
                dt.Price = price;
                dt.Quantity = quantity;
                _DeliveryNoteDetailSrv.Save(dt);

                totalprice += Convert.ToInt64(price) * Convert.ToInt64(quantity);

                Stock Stockupdate = _StockSrv.Getbykey(Convert.ToInt32(idproduct));
                Stockupdate.Quantity = (Convert.ToInt32(Stockupdate.Quantity) - Convert.ToInt32(quantity)).ToString();
                _StockSrv.AddOrUpdate(Stockupdate);

            }
            a.TotalPrice = totalprice.ToString();
            _DeliveryNoteSrv.AddOrUpdate(a);

            DialogResult = DialogResult.OK;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5)
            {
                int selectedIndex = dataGridView1.CurrentCell.RowIndex;
                rs.RemoveAt(selectedIndex);
                refreshDataGrid();
            }
        }

        

    }
}
